<template>
    <div class="form-group">
        <label for="bgcolor">背景颜色</label>
        <div class="color-input-group">
            <input type="color" id="bgcolor" v-model="backgroundConfig.color" class="color-picker">
            <div class="range-input-group">
                <input type="range" id="bgOpacity" min="0" max="1" step="0.01" v-model="backgroundConfig.opacity"
                    @input="applyBackground" class="range-slider">
                <span class="range-value">{{ backgroundConfig.opacity }}</span>
            </div>
            <button class="primary-button" @click="applyBackground">设置</button>
            <button class="secondary-button" @click="setTransparent">去除背景色</button>
            <button class="secondary-button" @click="resetBackground">恢复原始</button>
        </div>
    </div>
</template>

<script setup>
import { ref,inject } from 'vue'
const backgroundConfig = inject('backgroundConfig')

// Emits
const emit = defineEmits([
    'apply-background',
    'set-transparent',
    'reset-background'
])

// 响应式数据
// const backgroundColor = ref('transparent')
// const backgroundOpacity = ref(1)

// 工具函数
function hexToRgba(hex, alpha) {
    const r = parseInt(hex.slice(1, 3), 16)
    const g = parseInt(hex.slice(3, 5), 16)
    const b = parseInt(hex.slice(5, 7), 16)
    return `rgba(${r}, ${g}, ${b}, ${alpha})`
}

// 方法
function applyBackground() {
    let color
    if (backgroundConfig.color === 'transparent') {
        color = 'transparent'
    } else if (backgroundConfig.color === '#ffffff') {
        color = `rgba(255, 255, 255, ${backgroundConfig.opacity})`
    } else if (backgroundConfig.color === '#000000') {
        color = `rgba(0, 0, 0, ${backgroundConfig.opacity})`
    } else {
        color = hexToRgba(backgroundConfig.color, backgroundConfig.opacity)
    }

    emit('apply-background', color)
}

function setTransparent() {
    backgroundConfig.color = 'transparent'
    emit('set-transparent')
}

function resetBackground() {
    // backgroundConfig.color = '#ffffff'
    // backgroundConfig.opacity = 1
    emit('reset-background')
}
</script>

<style scoped>
.form-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.form-group label {
    font-size: 14px;
    color: #333;
    font-weight: 500;
}

.color-input-group {
    display: flex;
    gap: 8px;
    align-items: center;
}

.color-picker {
    width: 40px;
    height: 30px;
    padding: 0;
    border: 1px solid #ddd;
    border-radius: 4px;
}

.range-input-group {
    display: flex;
    gap: 12px;
    align-items: center;
}

.range-slider {
    flex: 1;
    height: 6px;
    -webkit-appearance: none;
    background: #e1e1e1;
    border-radius: 3px;
    outline: none;
}

.range-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    background: #1890ff;
    border-radius: 50%;
    cursor: pointer;
}

.range-value {
    min-width: 40px;
    font-size: 14px;
    color: #666;
}

.primary-button,
.secondary-button {
    padding: 6px 12px;
    border-radius: 4px;
    border: none;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s;
}

.primary-button {
    background-color: #1890ff;
    color: white;
}

.primary-button:hover {
    background-color: #40a9ff;
}

.secondary-button {
    background-color: #f0f0f0;
    color: #333;
}

.secondary-button:hover {
    background-color: #d9d9d9;
}
</style>